import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { StaffInfoEntity } from '../../entity/info';
import { StaffInfoService } from '../../service/info';
import { OrgInfoEntity } from '../../../org/entity/info';

/**
 * 员工信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: StaffInfoEntity,
  service: StaffInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name', 'a.staffNo'],
    fieldEq: ['a.orgId', 'a.gender', 'a.status'],
    fieldLike: [],
    where: async ctx => {
      const { entryDateStart, entryDateEnd } = ctx.request.body;
      const where = [];
      if (entryDateStart) {
        where.push(['a.entryDate >= :entryDateStart', { entryDateStart }]);
      }
      if (entryDateEnd) {
        where.push(['a.entryDate <= :entryDateEnd', { entryDateEnd }]);
      }
      return where;
    },
    join: [
      {
        entity: OrgInfoEntity,
        alias: 'b',
        condition: 'a.orgId = b.id',
        type: 'leftJoin',
      },
    ],
    select: ['a.*', 'b.name as orgName'],
  },
})
export class AdminStaffInfoController extends BaseController {
  @Inject()
  staffInfoService: StaffInfoService;
}
